home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / applyFluidDiskCache.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  17.0 KB  |  541 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. // For development only
  18. proc int debugPrint( string $printThis )
  19. {
  20.     // print $printThis;
  21.     return 0;
  22. }
  23.  
  24. proc int kickFluid()
  25. // Forces the fluid to load by switching the current time
  26. // This is a workaround to bug 161829. 
  27. {
  28.     int $currentTime = `currentTime -q`;
  29.     currentTime 3;
  30.     refresh;
  31.     currentTime 1;
  32.     refresh;
  33.     currentTime $currentTime;
  34.     return 0;
  35. }
  36. // Remove above 
  37.  
  38. proc string applyFluidDiskCacheFile(
  39.     string $fluidShape,
  40.     string $diskCacheFile,
  41.     int $xResolution,
  42.     int $yResolution,
  43.     int $zResolution,
  44.     int $is2d,            // 1 means it's 2d, 0 means it's 3d
  45.     int $hasDensity, 
  46.     int $hasVelocity, 
  47.     int $hasTemperature, 
  48.     int $hasReaction,
  49.     int $hasColor,
  50.     int $hasTexture ) 
  51. //
  52. //    Description:
  53. //        Creates a disk cache node for the given fluid shape,
  54. //        or changes the existing cache node for the given fluid shape,
  55. //        to have the given $diskCachefile in the user's working dir.
  56. //
  57. //        Also copies the given diskCacheFile to the user's working dir.
  58. //
  59. //        Returns the name of the disk cache node that was created/modified.
  60. //
  61. //        Call like this:
  62. //            string $diskCacheNode = applyFluidDiskCacheFile( 
  63. //                "fluidShape1",
  64. //                "<fullpath>/data/faintCloud.ma_fluidShape.mcfi",
  65. //                40, 40, 1, 1,         // resolution is 40x40x1, is2d=true
  66. //                1, 1, 0 0);            // has density and velocity, no temp or reaction
  67. //                
  68. //        Pre-conditions:
  69. //            $diskCacheFile already exists!!!
  70. //
  71. {
  72.     
  73.     debugPrint(" debug - applying fluid disk cache file\n");
  74.     string $diskCacheNode = "";
  75.     int  $diskCacheNodeWasCreated = 0;
  76.  
  77.     if( !`exists saveFluidICCache` ) {
  78.         source "doSetFluidState.mel";
  79.     }
  80.  
  81.     // Create or find the disk cache node to modify
  82.     //
  83.     int $whatToSave[] = { 1, 1, 1, 1, 1, 1 };
  84.  
  85.     if( `connectionInfo -id ($fluidShape + ".diskCacheIC")` == 0 ) {
  86.         debugPrint(" debug - creating new disk cache node for " + $fluidShape + "\n");
  87.         // Create a disk cache node for the fluid shape
  88.         $diskCacheNode = saveFluidICCache( $fluidShape, $whatToSave );
  89.         // Check that the node was created
  90.         if( size($diskCacheNode) == 0 ) {
  91.             // return and the calling routine will clean up
  92.             return "";
  93.         } else {
  94.             $diskCacheNodeWasCreated = 1;
  95.         }
  96.     } else {
  97.         debugPrint(" debug - existing disk cache node \n");
  98.         // Get the name of the existing disk cache node
  99.         string $src = `connectionInfo -sfd ($fluidShape + ".diskCacheIC")`;
  100.         string $buffer[];
  101.         tokenize($src, ".", $buffer);
  102.         $diskCacheNode = $buffer[0];
  103.     }
  104.     debugPrint(" debug - disk cache node is: " + $diskCacheNode + "\n");
  105.  
  106.     // Get a unique file name for the cache file based
  107.     // on the user's working dir.
  108.     //
  109.     string $newFileName = uniqueCacheName( $fluidShape, ".mcfi" );
  110.     string $dir = `diskCache -q -tmp`;
  111.     int    $dirExists = (size( $dir ) > 0) && (`file -q -exists $dir`);
  112.  
  113.     debugPrint(" debug - disk cache dir: " + $dir + "\n");
  114.     if( !$dirExists ) {
  115.         debugPrint(" debug - disk cache dir not found/specified, using \"data\" \n" );
  116.         verifyWorkspaceFileRule( "diskCache", "data" );
  117.         string $diskCacheDir = `workspace -fileRuleEntry "diskCache"`;
  118.  
  119.         $dir = `workspace -q -rd` + $diskCacheDir;
  120.         if( !`file -q -exists $dir` ) {
  121.             debugPrint(" debug - disk cache dir does not exist, trying to create it\n");
  122.             // create the dir here
  123.             sysFile -makeDir $dir;
  124.         }
  125.     }
  126.  
  127.     string $newDiskCacheFileName = $dir + "/" + $newFileName;
  128.  
  129.     // Copy the given disk cache file to the user's working dir
  130.     //
  131.     string $copyCmd = ("cp " + $diskCacheFile + " " + $newDiskCacheFileName );
  132.     if( `about -nt` ) {
  133.         // For Windows, change this into a pathname with back slashes
  134.         $copyCmd = ("copy \"" + $diskCacheFile + "\" \"" + $newDiskCacheFileName + "\"" );
  135.         string $result = substituteAllString($copyCmd, "/", "\\");
  136.         $copyCmd = $result;
  137.     }
  138.  
  139.     system( $copyCmd );
  140.     debugPrint(" debug - Copying: " + $copyCmd + "\n");
  141.  
  142.     // If the new disk cache file was successfully created,
  143.     // change the disk cache node's .hiddenCacheName to be the 
  144.     // given filename.  (The .hiddenCacheName holds the location
  145.     // of the current state of the fluid.  On file->save this
  146.     // state will be copied over to the .cacheName attr and file
  147.     // in the user's data directory.)
  148.     //
  149.     if( `file -q -exists $newDiskCacheFileName` ) {
  150.         // Modify the permissions on the .mcfi file we just
  151.         // copied to the user's tmp directory...  The default
  152.         // presets we ship with might be read-only, but the
  153.         // copy we just created in the tmp dir had better be
  154.         // writable by the user, or else (s)he won't be able to
  155.         // do much with it!!
  156.         //
  157.         if( `about -nt` ) {
  158.             system( "attrib -r " + substituteAllString( $newDiskCacheFileName,
  159.                                                         "/", 
  160.                                                         "\\" ) );
  161.         } 
  162.         else {
  163.             system( "chmod u+w " + $newDiskCacheFileName );
  164.         }
  165.  
  166.         debugPrint(" debug - set .hiddenCacheName: " + $newDiskCacheFileName + "\n");
  167.         string $nameValue;
  168.         if( $dirExists ) {
  169.             // The diskCache temp dir exists and is usable.  
  170.             // Use an attr value that's relative to the temp dir.
  171.             //
  172.             $nameValue = $newFileName;
  173.         } else {
  174.             // The temp dir wasn't available, so we have to 
  175.             // use a full path name for the attr value.
  176.             //
  177.             $nameValue = $newDiskCacheFileName;
  178.         }   
  179.  
  180.         setAttr -type "string" ($diskCacheNode +".hiddenCacheName") $nameValue;
  181.     } else {
  182.         debugPrint(" debug - trouble copying disk cache file from " +
  183.             $diskCacheFile + " to " + $newDiskCacheFileName + "\n");
  184.         if( $diskCacheNodeWasCreated == 1 ) {
  185.             delete $diskCacheNode;
  186.         }
  187.         $diskCacheNode = "";
  188.         warning(" Could not copy disk cache file from " +
  189.             $diskCacheFile + " to " + $newDiskCacheFileName );
  190.  
  191.         // Return here and the calling routine will clean up
  192.         return $diskCacheNode;
  193.     }
  194.  
  195.     // Check compatibility of the fluid shape and the data in the
  196.     // diskCacheFile.  It would be better to check BEFORE connecting
  197.     // these two, but that's not currently possible.
  198.     //
  199.     // Get the resolution from the fluid shape and fluid cache
  200.     //
  201.     int $compatible = 1;
  202.     int $fluidResW = `getAttr ($fluidShape + ".resolutionW ")`;
  203.     int $fluidResH = `getAttr ($fluidShape + ".resolutionH ")`;
  204.     int $fluidResD = `getAttr ($fluidShape + ".resolutionD ")`;
  205.     int $cacheRes[] = { $xResolution, $yResolution, $zResolution };
  206.     int $fluidIs2d = `getAttr ($fluidShape + ".is2d")`;
  207.     debugPrint(" debug - target resolution is " + 
  208.              $fluidResW  + "," + $fluidResH  + "," + $fluidResD  + "\n"  );
  209.  
  210.     if( ($fluidResW == $cacheRes[0]) &&
  211.          ($fluidResH == $cacheRes[1]) &&
  212.          ($fluidResD == $cacheRes[2]) && 
  213.         ($fluidIs2d == $is2d)    )  {
  214.  
  215.         debugPrint(" debug - Resolution matches, nothing left to do \n");
  216.         // Resolution of the fluid shape and disk cache file
  217.         // matches exactly.  No conversion/resamplng is needed
  218.         //
  219.         $compatible = 1;
  220.  
  221.     } else {
  222.  
  223.         if( $fluidIs2d == $is2d ) {
  224.             // Resolution of the fluid shape doesn't match resolution
  225.             // of the disk cache file.  Some conversion is needed
  226.             //
  227.             debugPrint(" debug - Resolution doesnt match, need to resample\n");
  228.             $compatible = 2;
  229.         } else {
  230.  
  231.             // Dimension is different.  Just give up here.
  232.             $compatible = 3;
  233.         }
  234.     }
  235.  
  236.     if( ($compatible == 1) || ($compatible == 2) ) {
  237.         // If cache file defines any of the following, then change the 
  238.         // shape's matching method to be dynamic grid (or static grid?).
  239.         // Note that if fluid shape doesn't allow loading density, then
  240.         // don't change its method.
  241.         //
  242.         if( $hasDensity == 1 && `getAttr  ($fluidShape + ".loadDensity")`) {
  243.             string $makeGridCmd[];
  244.             if( !fluidsVerifyGrid( $fluidShape, "density", $makeGridCmd ) ) {
  245.                 debugPrint(" debug - setting density method dynamic\n");
  246.                 evalEcho $makeGridCmd[0];
  247.             }
  248.         }
  249.         if( $hasVelocity == 1 && `getAttr  ($fluidShape + ".loadVelocity")`) {
  250.             string $makeGridCmd[];
  251.             if( !fluidsVerifyGrid( $fluidShape, "velocity", $makeGridCmd ) ) {
  252.                 debugPrint(" debug - setting velocity method dynamic\n");
  253.                 evalEcho $makeGridCmd[0];
  254.             }
  255.         }
  256.         if( $hasTemperature == 1 && `getAttr  ($fluidShape + ".loadTemperature")`) {
  257.             string $makeGridCmd[];
  258.             if( !fluidsVerifyGrid( $fluidShape, "temperature", $makeGridCmd )) 
  259.             {
  260.                 debugPrint(" debug - setting temperature method dynamic\n");
  261.                 evalEcho $makeGridCmd[0];
  262.             }
  263.         }
  264.         if( $hasReaction == 1 && `getAttr  ($fluidShape + ".loadReaction")`) {
  265.             string $makeGridCmd[];
  266.             if( !fluidsVerifyGrid( $fluidShape, "fuel", $makeGridCmd ) ) {
  267.                 debugPrint(" debug - setting fuel method dynamic\n");
  268.                 evalEcho $makeGridCmd[0];
  269.             }
  270.         }
  271.         if( $hasColor == 1 && `getAttr  ($fluidShape + ".loadColor")`) {
  272.             string $makeGridCmd[];
  273.             if( !fluidsVerifyGrid( $fluidShape, "color", $makeGridCmd ) ) {
  274.                 debugPrint(" debug - setting color method dynamic\n");
  275.                 evalEcho $makeGridCmd[0];
  276.             }
  277.         }
  278.         if( $hasTexture == 1 && `getAttr  ($fluidShape + 
  279.                                            ".loadTextureCoordinates")`) 
  280.         {
  281.             string $makeGridCmd[];
  282.             if( !fluidsVerifyGrid( $fluidShape, "textureCoordinates", 
  283.                                    $makeGridCmd ) ) 
  284.             {
  285.                 debugPrint(" debug - setting textureCoordinates method dynamic\n");
  286.                 evalEcho $makeGridCmd[0];
  287.             }
  288.         }
  289.     }
  290.  
  291.     // Load the fluid shape wth the disk cache node 
  292.     //
  293.     if( $compatible == 1 ) 
  294.     {
  295.         // This means that the diskCacheFile and fluid shape match in resolution
  296.         // and dimension.  No conversion/resampling needs to be done.
  297.         // Force the new fluid to load 
  298.         //
  299.         loadFluid -ic $fluidShape;
  300.  
  301.         kickFluid();
  302.     } 
  303.     else if( $compatible == 2 ) 
  304.     {
  305.         // At this point, the fluid shape and disk cache node are connected,
  306.         // but they have different resolutions
  307.  
  308.         // Convert the given disk cache resolution to match the fluid shape
  309.         // ie. different resolution
  310.         //
  311.         // 1.  resample fluid shape to match the disk cache
  312.         // 2.  load fluid
  313.         // 3.  resample fluid shape to original fluid resolution 
  314.         // 4.  save initial conditions to re-create the disk cache file
  315.         // get the resolution from the fluid cache
  316.  
  317.         // resample the fluid to disk cache node's resolution and load it
  318.         // This is the only way to get the disk cache info out.
  319.         //
  320.         resampleFluid -rw $cacheRes[0] -rh $cacheRes[1] -rd $cacheRes[2] $fluidShape;
  321.         debugPrint(" debug - resample fluid to " + 
  322.              $cacheRes[0]  + "," + $cacheRes[1]  + "," + $cacheRes[2]  + "\n"  );
  323.  
  324.          loadFluid -ic $fluidShape;
  325.         debugPrint(" debug - loading fluid \n");
  326.  
  327.         kickFluid();
  328.  
  329.         // Now properly force resampling of the fluid shape to
  330.         // the original desired fluid shape resolution
  331.         //
  332.         if( !`optionVar -query initialStatesResizeOnDrop` ) {
  333.             resampleFluid -rw $fluidResW -rh $fluidResH -rd $fluidResD $fluidShape;
  334.             debugPrint(" debug - resample fluid to " + 
  335.                        $fluidResW  + "," + $fluidResH  + "," + $fluidResD  + "\n"  );
  336.         }
  337.  
  338.         // Save initial conditions, with new resolution.  This will
  339.         // replace any old IC cache.
  340.         //
  341.         debugPrint(" debug - saving fluid cache\n");
  342.         saveFluidICCache( $fluidShape, $whatToSave );
  343.         //select -r $fluidShape;
  344.         //$diskCacheNode = `performFluids 1 FluidSave 0`;
  345.  
  346.     }
  347.     else 
  348.     {
  349.         // No hope of applying disk cache to this fluid
  350.         // UNDO whatever was created, incl. delete the temporary fluid
  351.         // cache, and restore the prevous one (if there was one)
  352.         //
  353.         if( $diskCacheNodeWasCreated == 1 ) {
  354.             delete $diskCacheNode;
  355.         }
  356.  
  357.         // Delete the temporary fluid cache
  358.         //
  359.         sysFile -delete $newDiskCacheFileName;
  360.         debugPrint(" debug - Aborting: " + $newDiskCacheFileName + "\n");
  361.  
  362.         // Restore the previous cache??
  363.  
  364.         $diskCacheNode = "";
  365.     }
  366.  
  367.     return $diskCacheNode;
  368. }
  369.  
  370. proc string createFluidShapeForDiskCache( int $xResolution, 
  371.                                           int $yResolution, 
  372.                                           int $zResolution, 
  373.                                           int $is2d )
  374. {
  375.     // Creates a fluid shape that matches the given
  376.     // disk cache in resolution
  377.     //
  378.     string $fluidResult = "";
  379.     if( $is2d ) {
  380.         $fluidResult = `performFluids 1 Create2DFluid 0`;
  381.     } else {
  382.         $fluidResult = `performFluids 1 Create3DFluid 0`;
  383.     }
  384.     string $selectionList[] = `ls -sl`;
  385.     if( size($selectionList) != 1 ) { 
  386.         return "";
  387.     }
  388.     $fluidResult = $selectionList[0];
  389.  
  390.     // Get the fluid shape underneath
  391.     string $fluidSh[];
  392.     if( size($fluidResult) > 0 ) {
  393.         $fluidSh = `listRelatives -children $fluidResult`;    
  394.     }
  395.     if( size($fluidSh) == 0 ) {
  396.         delete $fluidResult;
  397.         return "";
  398.     }
  399.  
  400.     // Set the attributes on the fluid shape to match the disk cache file
  401.     //
  402.     setAttr ($fluidSh[0] + ".resolutionW") $xResolution;
  403.     setAttr ($fluidSh[0] + ".resolutionH") $yResolution;
  404.     setAttr ($fluidSh[0] + ".resolutionD") $zResolution;
  405.     setAttr ($fluidSh[0] + ".is2d") $is2d;
  406.  
  407.     return $fluidResult;
  408. }
  409.  
  410.  
  411. global proc string applyFluidDiskCache( 
  412.     string $diskCacheFile, 
  413.     int $createFluidShape,
  414.     int $xResolution,
  415.     int $yResolution,
  416.     int $zResolution,
  417.     int $is2d,                // 1 means it's 2d, 0 means it's 3d
  418.     int $hasDensity, 
  419.     int $hasVelocity, 
  420.     int $hasTemperature, 
  421.     int $hasReaction,
  422.     int $hasColor,
  423.     int $hasTexture ) 
  424. //
  425. //    Description:
  426. //        This proc does all the work to connect the given disk cache
  427. //        file to a fluid shape.  If $createFluidShape is 1, then
  428. //        a fluid shape is created based on the disk cache file.
  429. //        Otherwise, the disk cache file is appled to the first
  430. //        selected fluid shape and converted (in the case of
  431. //        different resolution) as necessary.
  432. //
  433. {
  434.  
  435.     // Check if the given diskCacheFile exists.  A diskCache file
  436.     // string beginning with $MAYA_LOCATION should be expanded 
  437.     //
  438.     string $realCacheFile = substitute( "$MAYA_LOCATION", $diskCacheFile,
  439.                                         getenv( "MAYA_LOCATION" ) );
  440.  
  441.     // Substitution occurred and now we have a real
  442.     // path to the cache file.
  443.     //
  444.     if( $realCacheFile != $diskCacheFile ) {
  445.         $diskCacheFile = $realCacheFile;
  446.     }
  447.  
  448.     if( !`file -q -exists $diskCacheFile` ) {
  449.         error("Could not find disk cache file: " + $diskCacheFile);
  450.         return "";
  451.     }
  452.  
  453.     string $selectionList[] = `ls -sl`;
  454.     int $numSelected = size($selectionList);
  455.     int $fluidShapeCreated = 0;
  456.     string $fluidShape;
  457.  
  458.     // Make sure that we have a fluid selected
  459.     // or warn the user and fail
  460.  
  461.     if( $createFluidShape == 0 ) {
  462.            if ($numSelected == 0)
  463.            {
  464.             string $warn = "You must select a ";
  465.             if( $is2d == 1 ) {
  466.                 $warn += "2D ";
  467.             } else {
  468.                 $warn += "3D ";
  469.             }
  470.             $warn += "fluid to apply this preset to.";
  471.  
  472.             error($warn);
  473.             return "";
  474.            }
  475.     } else {
  476.         // Create a fluid shape based on the given disk cache file
  477.         // Reset the selection list.
  478.         //
  479.         $fluidResult = createFluidShapeForDiskCache( $xResolution, 
  480.                                                      $yResolution, 
  481.                                                      $zResolution, 
  482.                                                      $is2d );
  483.         if( $fluidResult == "" ) {
  484.             error("Unable to create a fluid shape to apply fluid disk cache to.");
  485.             return "";
  486.         }
  487.         $selectionList = `ls -sl`;
  488.         $fluidShapeCreated = 1;
  489.     }
  490.  
  491.        $fluidShape = getFluidShape($selectionList[0]);
  492.        if (size($fluidShape) == 0)
  493.        {
  494.            error("You must select a fluid to apply this preset to.");
  495.            return "";
  496.        }
  497.  
  498.     debugPrint (" debug - apply: " + $diskCacheFile + " to fluid " + $fluidShape + "\n");
  499.  
  500.     // Preliminary checks for whether the dimension matches.
  501.     // This is always true if we created the fluid above.
  502.     //
  503.     int $fluidIs2d = `getAttr ($fluidShape + ".is2d")`;
  504.     if( ($fluidIs2d == 1) && ($fluidIs2d != $is2d) ) {
  505.            error("Cannot apply 3d fluid disk cache to 2d fluid shape.");
  506.            return "";
  507.     }
  508.     if( ($fluidIs2d == 0) && ($fluidIs2d != $is2d) ) {
  509.            error("Cannot apply 2d fluid disk cache to 3d fluid shape.");
  510.            return "";
  511.     }
  512.  
  513.     // Connect and convert the disk cache file as necessary
  514.     //
  515.     string $result = applyFluidDiskCacheFile( $fluidShape, $diskCacheFile,
  516.                         $xResolution, $yResolution, $zResolution, $is2d,
  517.                         $hasDensity, $hasVelocity, $hasTemperature, 
  518.                         $hasReaction, $hasColor, $hasTexture );
  519.     if( $result == "" ) {
  520.         if( $fluidShapeCreated == 1 ) {
  521.             delete $fluidShape;
  522.         }
  523.         error("Cannot apply preset: " + $diskCacheFile + " to fluid " + $fluidShape );
  524.         return "";
  525.     }
  526.  
  527.     // Select the fluid transform and the disk cache node
  528.     //
  529.     string $fluidTransform[] = `listRelatives -parent $fluidShape`;
  530.     if( size($fluidTransform) == 0 ) 
  531.     {
  532.         error("Fluid shape " + $fluidShape + " has no parent transform.  Could not apply preset." );
  533.         return "";
  534.     }
  535.  
  536.     select -r $fluidTransform[0] $result;
  537.  
  538.     return ($fluidTransform[0] + " " + $result);
  539. }
  540.  
  541.